// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.butler.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.butler.controller.request.ImportTrainInfoReq;
import com.liuxinlong.modules.butler.controller.request.QueryFaqReq;
import com.liuxinlong.modules.butler.controller.request.UpdateTrainInfoReq;
import com.liuxinlong.modules.butler.controller.request.UpdateTrainStatusReq;
import com.liuxinlong.modules.butler.service.ButlerService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.ButlerTrain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 智能管家控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-09-27
 */
@RestController
@RequestMapping("/butler/train")
public class ButlerController {
    @Autowired
    private ButlerService butlerService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询信息
     *
     * @param req 请求信息
     * @return 培训文档信息列表
     */
    @PostMapping("page")
    public BaseResp pageButlerTrain(@RequestBody QueryFaqReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("keyword", req.getKeyword());
        queryMap.put("status", req.getStatus());
        queryMap.put("businessType", req.getBusinessType());
        List<Map<String, Object>> dataList = butlerService.pageTrainInfo(queryMap);
        int totalNum = butlerService.getTrainInfoCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询培训文档信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 分页查询信息
     *
     * @param req 请求信息
     * @return 培训文档信息列表
     */
    @PostMapping("query")
    public BaseResp queryButlerTrain(@RequestBody QueryFaqReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("keyword", req.getKeyword());
        queryMap.put("status", req.getStatus());
        queryMap.put("businessType", req.getBusinessType());
        queryMap.put("subType", req.getSubType());
        List<Map<String, Object>> dataList = butlerService.pageTrainInfo(queryMap);
        int totalNum = butlerService.getTrainInfoCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询培训文档信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增培训文档信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增培训文档信息", business = LogBusinessConstants.BUTLER_MANAGE, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addTrainInfo(@RequestBody UpdateTrainInfoReq req) {
        ButlerTrain butlerTrain = new ButlerTrain();
        butlerTrain.setTitle(req.getTitle());
        butlerTrain.setCover(req.getCover());
        butlerTrain.setBusinessType(req.getBusinessType());
        butlerTrain.setSubType(req.getSubType());
        butlerTrain.setContentType(req.getContentType());
        butlerTrain.setShowIndex(req.getShowIndex());
        butlerTrain.setJumpUrl(req.getJumpUrl());
        butlerTrain.setRelation(req.getRelation());
        butlerTrain.setContent(req.getContent());
        butlerTrain.setMainFile(req.getMainFile());
        butlerTrain.setMainFilePdf(req.getMainFilePdf());
        butlerTrain.setAttach(req.getAttach());
        butlerTrain.setShareUser(req.getShareUser());
        butlerService.addTrainInfo(butlerTrain,req.getForceFlag());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增培训文档信息成功");
        return resp;
    }

    /**
     * 修改培训文档信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改培训文档信息", business = LogBusinessConstants.BUTLER_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateQTrainInfo(@RequestBody UpdateTrainInfoReq req) {
        ButlerTrain butlerTrain = new ButlerTrain();
        butlerTrain.setId(req.getId());
        butlerTrain.setTitle(req.getTitle());
        butlerTrain.setCover(req.getCover());
        butlerTrain.setBusinessType(req.getBusinessType());
        butlerTrain.setSubType(req.getSubType());
        butlerTrain.setContentType(req.getContentType());
        butlerTrain.setShowIndex(req.getShowIndex());
        butlerTrain.setJumpUrl(req.getJumpUrl());
        butlerTrain.setRelation(req.getRelation());
        butlerTrain.setContent(req.getContent());
        butlerTrain.setMainFile(req.getMainFile());
        butlerTrain.setMainFilePdf(req.getMainFilePdf());
        butlerTrain.setAttach(req.getAttach());
        butlerTrain.setShareUser(req.getShareUser());
        butlerService.updateTrainInfo(butlerTrain,req.getForceFlag());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改培训文档信息成功");
        return resp;
    }

    /**
     * 删除培训文档信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除培训文档信息", business = LogBusinessConstants.BUTLER_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteTrainInfo(@RequestBody BaseDeleteReq req) {
        butlerService.deleteTrainInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除培训文档信息成功");
        return resp;
    }

    /**
     * 导出培训文档信息
     *
     * @param req 请求信息
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出培训文档信息", business = LogBusinessConstants.BUTLER_MANAGE,operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportTrainInfo(@RequestBody BaseDeleteReq req, HttpServletResponse response) {
        butlerService.exportTrainInfo(req.getId(), response);
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value = "导入培训数据", business = LogBusinessConstants.BUTLER_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importTrainInfo(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = butlerService.importTrainInfo((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入培训文档信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value = "导入培训数据", business = LogBusinessConstants.BUTLER_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddTrainInfo(@RequestBody ImportTrainInfoReq req) {
        return butlerService.batchAddTrainInfo(req.getDataList());
    }

    /**
     * 修改培训文档状态
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("status-update")
    @LogAnnotation(value = "修改培训文档状态", business = LogBusinessConstants.BUTLER_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateTrainInfoStatus(@RequestBody UpdateTrainStatusReq req) {
        butlerService.updateTrainInfoStatus(req.getId(),req.getPushFlag());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改培训文档状态成功");
        return resp;
    }

    /**
     * 记录点击次数
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("count")
    @LogAnnotation(value = "记录点击次数", business = LogBusinessConstants.BUTLER_MANAGE, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp countClick(@RequestBody BaseDeleteReq req) {
        butlerService.countClick(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("记录点击次数信息成功");
        return resp;
    }
}
